/*
 * Electronics.cpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   Electronics.cpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   5 mar 2012
 */

#include <stdexcept>

#include "Thermistor.hpp"
#include "Circuit.hpp"
#include "AnalogToDigitalConverter.hpp"

#include "Electronics.hpp"

namespace tltgen {

  Electronics::Electronics(const Thermistor *thermistor, const Circuit *circuit, const AnalogToDigitalConverter *adc) :
    thermistor(thermistor), circuit(circuit), adc(adc)
  {
    if (thermistor == 0) {
      throw std::invalid_argument("The given thermistor is NULL.");
    }
    if (circuit == 0) {
      throw std::invalid_argument("The given circuit is NULL.");
    }
    if (adc == 0) {
      throw std::invalid_argument("The given ADC is NULL.");
    }
  }

  Electronics::~Electronics() 
  { }

  const Thermistor* Electronics::getThermistor() const
  {
    return this->thermistor;
  }

  const Circuit* Electronics::getCircuit() const
  {
    return this->circuit;
  }

  const AnalogToDigitalConverter* Electronics::getAnalogToDigitalConverter() const
  {
    return this->adc;
  }


  Temperature Electronics::getTemperature(AdcValue adcValue) const
  {
    Voltage v = getAnalogToDigitalConverter()->getInputVoltage(adcValue);
    Resistance r = getCircuit()->getThermistorResistance(v);
    return getThermistor()->getTemperature(r);
  }



  int Electronics::getAdcOutputValue(Temperature t) const
  {
    Resistance r = getThermistor()->getResistance(t);
    Voltage v = getCircuit()->getOutputVoltage(r);
    return getAnalogToDigitalConverter()->getOutputValue(v);
  }

}

